<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../js/vue.js"></script>
    <script src="../js/datetime_utils.js"></script>
    <style>
        .grid {
            margin: 100px auto;
            width: 1000px;
            text-align: center;
        }

        .grid table {
            width: 100%;
            border-collapse: collapse;
        }

        .grid th {
            background-color: orange;

        }

        .grid th,td {
            border: 1px solid orange;
            height: 35px;
            line-height: 35px;

        }

        .grid .dept_input {
            margin-bottom: 5px;
            padding-top: 15px;
            background-color: pink;

        }
    </style>
</head>
<body>
<div id="div1">

    <div class="grid">
        <h2>大标题</h2>
        <div class="dept_input">
        <div>
            编号：<input type="text" name="id1" v-model="id" :disabled="flag">
            名称：<input type="text" name="name" v-model="name">
            <button @click="handle" :disabled="submitControl"  >保存</button>
        </div>
            <span>当前有多少个</span>
            <span>{{sum}}</span>
    </div>

    <table>
        <tr>
            <th>编号</th>
            <th>名称</th>
            <th>时间</th>
            <th>操作</th>
        </tr>

        <tr v-for="item in departments">
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>{{item.date}}</td>
            <td>
                <a href="" @click.prevent="edit(item.id)">修改</a>
                <span>|</span>
                <a href="" @click.prevent="deleteDept(item.id)">删除</a>

            </td>
        </tr>
    </table>
    </div>
</div>

<script>
    const vueObj = new Vue({
        el: "#div1",
        data: {
            flag: false,
            id: "",
            name: "",
            submitControl: false,
            departments: [
                {
                    id: 1,
                    name: "采购部",
                    date: "2022-02-12",
                },
                {
                    id: 2,
                    name: "销售部",
                    date: "2022-02-12",
                },
                {
                    id: 3,
                    name: "技术部",
                    date: "2022-02-12",
                },
                {
                    id: 4,
                    name: "摸鱼部",
                    date: "2022-02-12",
                }
            ],
        },

        methods: {
            handle: function () {
                // console.log("jj");

                if (this.flag) {
                    this.departments.some((item) => {
                        if (item.id == this.id) {
                            item.name = this.name;
                            return true;
                        }
                    });
                    this.flag = false;
                } else {
                    const dept = {};
                    dept.id = this.id;
                    dept.name = this.name;
                    dept.date = new Date();

                    this.departments.push(dept);
                }
                this.id = "";
                this.name = "";
            },
            deleteDept: function (id) {
                this.departments = this.departments.filter(function (item) {
                    return item.id != id;
                });
            },

            edit: function (id) {
                this.flag = true;
                const current_dept = this.departments.filter(function (item) {
                    return item.id == id;

                });
                this.id = current_dept[0].id;
                this.name = current_dept[0].name;
            },
        },
        computed: {
            sum: function () {
                return this.departments.length;
            },
        },
        watch: {
            name: function (new_value) {
               const flag = this.departments.some(function (item) {
                    return item.name == new_value;
                });

                if (flag) {
                    this.submitControl = true;
                } else {
                    this.submitControl = false;
                }
            },
        },
    });
</script>

</body>
</html>